<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>配置参数新增</title>
    <link href="../../layui/css/layui.css" rel="stylesheet">
    <link href="../../static/css/style.css" rel="stylesheet">
    <script src="../../static/js/jquery.min.js"></script>
    <script src="../../static/js/jquery.cookie.js"></script>
    <script src="../../layui/layui.js"></script>
    <script src="../../static/js/base.js"></script>
    <script src="../../static/js/ax.js"></script>
    <script src="../../static/js/avalon.js"></script>
    <!--[if lt IE 9]>
    <script src="../../static/js/html5.js"></script>
    <script src="../../static/js/respond.js"></script>
    <![endif]-->

</head>

<body class="bg_w">
    <div class="content-wrap" ms-controller="test2">
        <div class="layui-row">
            <form class="layui-form layui-form-pane" id="from1">

                <div class="layui-row layui-col-space10">
                    <div class=" layui-col-sm6">
                        <label class="layui-form-label">省份</label>
                        <div class="layui-input-block">
                            <select lay-filter="provinceSelect" id='driverprovinceId' lay-verify="required">
                                <option value="">请选择</option>
                                <option ms-attr="{value: el.code}" ms-attr="{name: el.name}" ms-for="($index, el) in @provlist">{{el.name}}</option>
                            </select>
                        </div>
                    </div>

                    <div class="layui-col-sm6">
                        <label class="layui-form-label">市区</label>
                        <div class="layui-input-block">
                            <select lay-filter="regionSelect" id='drivercityCode' lay-verify="required" name="cityCode">
                                <option value="">请选择</option>
                                <option ms-attr="{value: el.code}" ms-attr="{name: el.name}" ms-for="($index, el) in @citylist">{{el.name}}</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-col-sm6">
                        <label class="layui-form-label">参数配置类型</label>
                        <div class="layui-input-block">
                            <select lay-filter="typeSelect" id='drivercityCode' :duplex="configType" lay-verify="required" name="cityCode">
                                <option value="manual">人工导入导出</option>
                                <option value="api">api通道</option>
                                <option value="ftp">ftp通道</option>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="layui-row layui-col-space10 configlist">

                    <div class="layui-col-sm8 list" ms-for="($index, el) in @configlist" :if="@el.configType==@configType">
                        <label class="layui-form-label">{{el.itemName}}</label>
                        <div class="layui-input-block">
                            <input type="text" name="itemValue"   lay-verify="required" class="layui-input vlal" ms-attr="{placeholder:'请输入'+ el.itemName}">
                            <input class="type" type="hidden" ms-attr="{value:el.itemType}" >
                            <input class="name" type="hidden" ms-attr="{value:el.itemName}" >
                            
                        </div>
                    </div>


                </div>
                <div class="layui-input-block">
                    <button type="button" id="btnClose" class="layui-btn layui-btn-primary btn-left" :click="btnClose()">关闭</button>
                    <button type="button" id="btnSubmit" lay-submit lay-filter="component-form-element" class="layui-btn add_btn btn-left">提交</button>
                </div>
            </form>
        </div>
    </div>
</body>

<script>
    var form,layer;

    var vm = avalon.define({
        $id: "test2",
        provinceCode: '',
        cityCode: '',
        cityName:'',
        provinceName:'',
        provlist: [],
        citylist: [],
        arealist: [],
        infolist: [],
        pro_city_are: {},
        configlist: [],
        configType: "manual",
        //  查询省市区
        getRegionTree: function () {
            $.ajax({
                url: '../../static/js/addreess.text',  //文件地址
                type: 'GET',     //数据传递方式
                async: false,
                success: function (data) {  //获取数据成功执行的函数
                    var data = JSON.parse(data);
                    var city = data.city;
                    var area = data.area;
                    vm.provlist = data.prove;
                    vm.pro_city_are = data;
                    for (var i in city) {
                        if (city[i].parentId == data.prove[0].id) {
                            vm.citylist.push(city[i])
                        }
                    }
                    vm.cityCode = city[0].id;

                }
            })
        },
        // 查询所有配置项
        getconfiglist: function () {
            ax.get({
                url: govservicUrl + '/api/gov/ex/config/getConfigItemTemplateInfo',
                success: function (r) {
                    console.log(r)
                    vm.configlist = r.data
                },
            });
        },
        btnClose:function(){
            layer.closeAll("iframe"); 
        }


    })
    vm.$watch('onReady', function () {
        //获取省市区
        vm.getRegionTree();
        vm.getconfiglist();

    })
    var $;
    layui.use(['form', 'layer', 'laydate', 'upload'], function () {
        $ = layui.$;
        form = layui.form;
        layer = layui.layer;
        var layDateDscSignTime = layui.laydate,
            layDateDscBeginTime = layui.laydate,
            layDateDscEndTime = layui.laydate,
            uploadPicture = layui.upload,
            uploadOtherFile = layui.upload;


        form.on('submit(component-form-element)', function (data) {
            var datas={
            }
            datas.cityCode=vm.cityCode;
            datas.cityName=vm.cityName;
            datas.provinceCode=vm.provinceCode;
            datas.provinceName=vm.provinceName;
            datas.configType=vm.configType;
            var configItemList=[];

            $('.configlist .list').each(function(){
                configItemList.push({
                    itemType:$(this).find('.type').val(),
                    itemValue:$(this).find('.vlal').val(),
                    itemName:$(this).find('.name').val()
                })

            })
            datas.configItemList=configItemList
            console.log(datas)
            ax.post({
                url: govservicUrl + "/api/gov/ex/config/saveConfig",
                data:JSON.stringify(datas),
                success: function (data) {
                    layer.msg(data.message);
                    if (data.code == 0) {
                        //刷新父页面
                        parent.location.reload();
                        layer.closeAll("iframe");
                    } else {

                    }
                }
            });
            return false;
        })
        //省改变
        form.on('select(provinceSelect)', function (data) {
            vm.provinceCode = data.value;
            var city = vm.pro_city_are.city;
            vm.citylist.clear();
            for(var j in vm.pro_city_are.prove){
                if(vm.pro_city_are.prove[j].code==vm.provinceCode){
                    vm.provinceid = vm.pro_city_are.prove[j].id;
                }
            }
            for (var i in city) {
                if (city[i].parentId == vm.provinceid) {
                    vm.citylist.push(city[i])
                }
            }
           
            var  name=$(this).html();
             vm.provinceName=name; 
            console.log( vm.provinceCode)
            vm.cityCode = vm.citylist[0].id;
            form.render('select');

        });
        // 市改变
        form.on('select(regionSelect)', function (data) {
            var  name=$(this).html();
            vm.cityCode = data.value;
            vm.cityName = name;
        });
        // 类型改变
        form.on('select(typeSelect)', function (data) {
            vm.configType = data.value;

        });
         


    })

</script>


</html>